//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.com.api;

import cn.com.callback.CallBackInvoke;
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import cn.com.service.GeneralService;
import groovy.lang.GroovyClassLoader;

import java.io.File;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApiTaskBean {
    private static final Logger log = LoggerFactory.getLogger(EliteBean.class);
    private static GroovyClassLoader loader = Util.getGroovyClassLoader();
    private Class groovyClass = null;
    private String dataSourceID;
    private String sql;
    private EliteBean eliteBean;
    private String groovyPath;
    private String templetName;
    private VelocityEngine velocityEngine;

    public ApiTaskBean() {
    }

    public String getDataSourceID() {
        return this.dataSourceID;
    }

    public void setDataSourceID(String dataSourceID) {
        this.dataSourceID = dataSourceID;
    }

    public VelocityEngine getVelocityEngine() {
        return this.velocityEngine;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

    public String getGroovyPath() {
        return this.groovyPath;
    }

    public void setGroovyPath(String groovyPath) {
        this.groovyPath = groovyPath;
    }

    public String getSql() {
        return this.sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public EliteBean getEliteBean() {
        return this.eliteBean;
    }

    public void setEliteBean(EliteBean eliteBean) {
        this.eliteBean = eliteBean;
    }

    public String getTempletName() {
        return this.templetName;
    }

    public void setTempletName(String templetName) {
        this.templetName = templetName;
    }

    public void doTask() throws Exception {
        log.debug("groovyPath:" + this.groovyPath);
        if (this.groovyClass == null) {
            this.groovyClass = loader.parseClass(new File(GeneralService.groovyPath + File.separator + this.groovyPath));
        }

        CallBackInvoke invokeGroovy = (CallBackInvoke) this.groovyClass.newInstance();
        if (this.sql != null && !this.sql.equals("") && this.sql.length() > 0) {
            List list = this.eliteBean.chaxun_sql(this.sql, this.dataSourceID);
            log.debug("list.size()" + list.size());
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); ++i) {
                    try {
                        Map map = (Map) list.get(i);
                        String body = null;
                        if (this.templetName != null && !this.templetName.equals("") && this.templetName.length() > 0) {
                            Set set = map.keySet();
                            Iterator iter = set.iterator();
                            Context context = new VelocityContext();

                            while (iter.hasNext()) {
                                String key = (String) iter.next();
                                log.debug("key:" + key);
                                String value = (String) map.get(key);
                                log.debug("value:" + value);
                                context.put(key, value);
                            }

                            log.debug("templetName:" + this.templetName);
                            Template t = this.velocityEngine.getTemplate(this.templetName, "utf-8");
                            StringWriter writer = new StringWriter();
                            t.merge(context, writer);
                            body = writer.toString();
                        }

                        log.debug("body:" + body);
                        invokeGroovy.doCallBack(this.eliteBean, (String) null, map, body);
                    } catch (Exception var11) {
                        log.error(Util.getERRO(var11));
                    }
                }
            }
        } else {
            invokeGroovy.doCallBack(this.eliteBean, (String) null, (Map) null, (String) null);
        }

    }
}
